const app = getApp();
console.log(app);
// 例外不用token的地址
var exceptionAddrArr = [];

//请求头处理函数
function CreateHeader(url, type) {
  let header = {}
  if (type == 'POST_PARAMS') {
    header = {
      'content-type': 'application/x-www-form-urlencoded'
    }
  } else {
    header = {
      'content-type': 'application/json'
    }
  }
  //排除请求的地址不须要token的地址
  if (exceptionAddrArr.indexOf(url) == -1) {
    let token = app.token;
    // header.Authorization = token;
    header['access-token'] = token;
  }

  header['beid'] = app.beid;
  header['memberId'] = app.myInfo.memberId;
  header['token_type'] = 'c_token';
  return header;
}
//post请求，数据在body中
function postRequest(url, data) {
  let header = CreateHeader(url, 'POST');
  console.log(header, 'header')
  return new Promise((resolve, reject) => {
    wx.request({
      url: app.API_HOST + url,
      data: data,
      header: header,
      method: 'POST',
      success: (res => {
        if (res.statusCode === 200) {
          //200: 服务端业务处理正常结束
          if (res.data.code == 0) {
            resolve(res)
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              showCancel: false
            })
            reject(res)
          }
        } else if (res.statusCode >= 500) {
          wx.showToast({
            title: "网络错误",
            icon: 'none',
            duration: 2000
          })
          reject(res)
        } else if (res.statusCode >= 400) {
          wx.showToast({
            title: "请求错误",
            icon: 'none',
            duration: 2000
          })
          reject(res)
        } else {
          wx.showToast({
            title: res.data,
            icon: 'none',
            duration: 2000
          })
          reject(res)
        }
      }),
      fail: (res => {
        reject(res)
      }),
      complete() {

      },
    })
  })
}
//post请求，数据按照query方式传给后端
function postParamsRequest(url, data) {
  let header = CreateHeader(url, 'POST_PARAMS');
  let useurl = url;
  console.log(useurl);
  return new Promise((resolve, reject) => {
    wx.request({
      url: app.API_HOST + useurl,
      data: data,
      header: header,
      method: 'POST',
      success: (res => {
        console.log(res, '1212')
        if (res.statusCode === 200) {
          //200: 服务端业务处理正常结束
          if (res.data.code == 0) {
            resolve(res)
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              showCancel: false
            })
            reject(res)
          }
        } else if (res.statusCode >= 500) {
          wx.showToast({
            title: "网络错误",
            icon: 'none',
            duration: 2000
          })
          reject(res)
        } else if (res.statusCode >= 400) {
          wx.showToast({
            title: "请求错误",
            icon: 'none',
            duration: 2000
          })
          reject(res)
        } else {
          wx.showToast({
            title: res.data,
            icon: 'none',
            duration: 2000
          })
          reject(res)
        }
      }),
      fail: (res => {
        reject(res)
      }),
      complete() {

      },
    })
  })
}
//get 请求
function getRequest(url, data) {
  let header = CreateHeader(url, 'GET');
  return new Promise((resolve, reject) => {
    wx.request({
      url: app.API_HOST + url,
      data: data,
      header: header,
      method: 'GET',
      success: (res => {
        if (res.statusCode === 200) {
          //200: 服务端业务处理正常结束
          if (res.data.code == 0) {
            resolve(res)
          } else {

            if (res.data.message == ' 会员不存在') {
              wx.showModal({
                title: '提示',
                content: res.data.message,
                showCancel: false,
              })
              wx.clearStorage('token')
              wx.clearStorageSync("deskName")
              app.initLogin().then(res => {
                wx.request({
                  url: app.API_HOST + url,
                  data: {memberId: app.myInfo.memberId},
                  header: header,
                  method: 'GET',
                  success: (res2 => {
                    console.log(res2);
                    if (res2.data.code == 0) {
                      resolve(res2)
                    }else{
                      reject(res2)
                    }
                  })
                })
              }).catch(err => {})
            } else {
              wx.showModal({
                title: '提示',
                content: res.data.message,
                showCancel: false
              })
              reject(res)
            }

          }
        } else if (res.statusCode >= 500) {
          console.log('网络错误的链接',url)
          wx.showToast({
            title: "网络错误",
            icon: 'none',
            duration: 2000
          })
          reject(res)
        } else if (res.statusCode >= 400) {
          console.log('请求错误的链接',url)
          wx.showToast({
            title: "请求错误",
            icon: 'none',
            duration: 2000
          })
          reject(res)
        } else {
          console.log('其它错误的链接',url)
          wx.showToast({
            title: res.data,
            icon: 'none',
            duration: 2000
          })
          reject(res)
        }
      }),
      fail: (res => {
        reject(res)
      }),
      complete() {

      },
    })
  })
}

module.exports.getRequest = getRequest;
module.exports.postRequest = postRequest;
module.exports.postParamsRequest = postParamsRequest;